/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package experimentseswc;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.util.FileManager;
import java.io.File;
import java.io.IOException;
import java.util.Properties;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
/**
 *
 * @author gonzalez-l
 */
public class Catalog {

	//HashMap<String,String> catalog;
	Properties catalog;
	String execDir;
    String sparqlDir;
    boolean contactSource=false;

	public Catalog(){

		this.catalog = new Properties();
		this.execDir = "";
        this.sparqlDir = "";
	}

	public Catalog(Properties cat){

		this.catalog = cat;
	}

    public Catalog(Properties cat, String p, String q){

		this.catalog = cat;
		this.execDir = p;
        this.sparqlDir = q;
	}
	
	public Catalog(Properties cat, String p, String q, boolean c){

		this.catalog = cat;
		this.execDir = p;
        this.sparqlDir = q;
        this.contactSource = c;
	}

	public Model getModel(String str){

	    if (this.contactSource) {
            ProcessBuilder pb = new ProcessBuilder(this.execDir + "downloadresult.sh", 
                                                   this.catalog.getProperty(str), str);
            pb.directory(new File (this.execDir));
            try {
                Process p = pb.start();
                if(p.waitFor() != 0){
                    return null;
                }
            } catch (IOException ex) {
                ex.printStackTrace(System.out);
                return null;
            } catch (InterruptedException iex){
                iex.printStackTrace(System.out);
                return null;
            }
        }
        Model res = FileManager.get().loadModel(this.execDir+str+".n3");
        return res;
    }

    public Query getQuery(Predicate p, HashMap<String, String> constants) {
        /*
           leer el query asociado con el nombre del predicado p
           parsear el query
           hacer reemplazo de variables del query por los argumentos de p
         */
		try {
            String fileName = this.sparqlDir+p.getName()+".sparql";
            FileInputStream fis = new FileInputStream(fileName);
            QueryParser qp = new QueryParser(fis);
            Query q = qp.ParseSparql();
            ArrayList<String> existentialVars = q.getExistentialVars();
            ArrayList<String> arguments = new ArrayList<String>();
            for (String a : p.getArguments()) {
                arguments.add("?"+a);
            }
            existentialVars.retainAll(arguments);
            q.replaceAll(existentialVars);
            ArrayList<String> projectedVars = q.getVars();
            for (int k = 0; k < projectedVars.size(); k++) {
                String a = arguments.get(k);
                String v = projectedVars.get(k);
                q.replace(v, a);
            }
            for (String a : p.getArguments()) {
                if (constants.containsKey(a)) {
                    q.addBGP(new Filter(new BinaryExpression("=", new UnaryExpression("?"+a), new UnaryExpression(constants.get(a)))));
                }
            }
            return q;
        }  catch (Exception e) {
			e.printStackTrace(System.out);
			return null;
        }
    }
}
